package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.HomeSub;
import edu.fdzc.learn.service.HomeSubService;
import edu.fdzc.learn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/homesub")
public class HomeSubController {

    @Autowired
    private HomeSubService homeSubService;

    /**
     * 学生提交作业
     * @param homeworkId
     * @return
     */
    @PostMapping("/{homeworkId}/submit")
    public Result addHomeSub(@PathVariable int homeworkId, @RequestBody HomeSub homeSub) {
        homeSub.setHomeworkId(homeworkId);
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        homeSub.setStudentId(id);
        homeSubService.insert(homeSub);
        return Result.success();
    }

    @PutMapping("/{id}/update")
    public Result updateHomeSub(@PathVariable int id,@RequestBody HomeSub homeSub) {
        homeSub.setId(id);
        homeSubService.update(homeSub);
        return Result.success();
    }

    @GetMapping("/{studentId}/student")
    public Result<HomeSub> getHomeSub(@PathVariable int studentId) {
        HomeSub hs = homeSubService.findBySId(studentId);
        return Result.success(hs);
    }

    @GetMapping("{homeworkId}")
    public Result<List<HomeSub>> getAllHomeSub(@PathVariable int homeworkId) {
        List<HomeSub> list = homeSubService.findAll(homeworkId);
        return Result.success(list);
    }


}
